#include "stm32f10x.h"               
extern uint8_t flag_arrows,flag_sure,page_turner;

void Buttonexti_Init(void)
{
	RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA,ENABLE );
	RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB,ENABLE );
	RCC_APB2PeriphClockCmd (RCC_APB2Periph_AFIO,ENABLE );
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;	//设置上拉输入模式
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;	//设置上拉输入模式
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);//AFIOPIN2选择GPIOA口
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12);//AFIOPIN12选择GPIOB口
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource13);//AFIOPIN13选择GPIOB口
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);//AFIOPIN14选择GPIOB口

	EXTI_InitTypeDef EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line = EXTI_Line2|EXTI_Line12|EXTI_Line13|EXTI_Line14;//选择PIN2
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;	//开启中断
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;//中断响应
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;//上升沿触发
	EXTI_Init(&EXTI_InitStruct);
	
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel =EXTI2_IRQn;	//指定通道
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;			//通道使能开启
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级设置，分组2取值0，1，2，3.
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;		//响应优先级设置，分组2取值0，1，2，3.
	NVIC_Init(&NVIC_InitStruct);
	NVIC_InitStruct.NVIC_IRQChannel =EXTI15_10_IRQn;	//指定通道
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;			//通道使能开启
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级设置，分组2取值0，1，2，3.
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;		//响应优先级设置，分组2取值0，1，2，3.
	NVIC_Init(&NVIC_InitStruct);
}


void EXTI2_IRQHandler(void)//上键
{
	if(EXTI_GetITStatus(EXTI_Line2)==SET)	//判断是否是通道引起中断
	{
		flag_arrows--;
		if (page_turner > 0) page_turner--;
		if(flag_arrows == 0) flag_arrows = 3;
		EXTI_ClearITPendingBit(EXTI_Line2);	//清零中断标志位
	}
}


void EXTI15_10_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line12)==SET)		//下键
	{
		flag_arrows++;
		page_turner++;
		if(flag_arrows == 4) flag_arrows = 1;
		EXTI_ClearITPendingBit(EXTI_Line12);	//清零中断标志位
	}	
	
	if(EXTI_GetITStatus(EXTI_Line13)==SET)		//确定键
	{
		flag_sure = 1;
		EXTI_ClearITPendingBit(EXTI_Line13);	//清零中断标志位
	}	
	
	if(EXTI_GetITStatus(EXTI_Line14)==SET)		//返回键
	{
		flag_sure = 2;
		EXTI_ClearITPendingBit(EXTI_Line14);	//清零中断标志位
	}
}


